package com.lw.main;

import com.lw.main.word.GameTime;
import com.lw.util.Constant;
import com.lw.util.GameUtil;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 障碍物图层
 */
public class GameBarrierLayer {
    private Random random = new Random();

    private List<Barrier> barriers;

    private int numberTop;//上方障碍物高度
    private int numberDown;//下方障碍物高度
    private int number;

    public GameBarrierLayer() {
        barriers = new ArrayList<>();
    }

    /**
     * 绘制障碍物
     */
    public void draw(Graphics g, Bird bird) {
        this.collideBird(bird);
        for (int i = 0; i < barriers.size(); i++) {
            Barrier barrier = barriers.get(i);
            if (barrier.isVisible()) {
                barrier.draw(g);
            } else {//离开了屏幕就归还给对象池
                Barrier remove = barriers.remove(i);
                BarrierPool.setPool(remove);
                i--;
            }
        }
        this.logic(g);

//        this.printlnBarrierLocation();
    }

    private void printlnBarrierLocation() {
        System.out.println();
        for (Barrier barrier : barriers) {
            System.out.print(String.format("(%d,%d),", barrier.getX(), barrier.getY()));
        }
    }

    /**
     * 障碍物生成和销毁
     */
    public void logic(Graphics g) {
        if (barriers.size() == 0) {
            ran();
            this.insert(Constant.WIDTH, 0, numberTop, Barrier.TOP_NORMAL);
            this.insert(Constant.WIDTH, Constant.HEIGHT - numberDown, numberDown, Barrier.BOTTOM_NORMAL);

        } else {
            Barrier last = barriers.get(barriers.size() - 1);
            if (last.willCreateNext()) {
                ran();

                if (number < 50) {//悬浮障碍物
                    this.insert(Constant.WIDTH, 60, random.nextInt(150,201), Barrier.HOVER_NORMAL);
                } else if (number > 450) {//会移动的障碍物
                    this.insert(Constant.WIDTH, 125, random.nextInt(120,201), Barrier.MOVEABLE);
                } else {
                    this.insert(Constant.WIDTH, 0, numberTop, Barrier.TOP_NORMAL);
                    this.insert(Constant.WIDTH, Constant.HEIGHT - numberDown, numberDown, Barrier.BOTTOM_NORMAL);
                }
            }
        }
    }

    /**
     * 随机产生两个[100,500)的随机高度
     */
    public void ran() {
        this.numberTop = random.nextInt(100,Constant.HEIGHT/2-25);//[100,500/2-25)
        this.numberDown = random.nextInt(100,Constant.HEIGHT/2-25);//[100,500/2-25)

        this.number = random.nextInt(Constant.HEIGHT);//[0,500)
    }

    /**
     * 从池中获取对象
     *
     * @param x
     * @param y
     */
    public void insert(int x, int y, int height, int type) {
        Barrier top = BarrierPool.getPool();
        top.setX(x);
        top.setY(y);
        top.setHeight(height);
        top.setType(type);
        top.setVisible(true);
        barriers.add(top);
    }

    /**
     * 判断小鸟和障碍物是否碰撞
     */
    public boolean collideBird(Bird bird) {
        for (int i = 0; i < barriers.size(); i++) {
            Barrier barrier = barriers.get(i);
            if (barrier.getRect().intersects(bird.getRect())) {//判断矩形是否相交
//                bird.setLife(false);
                return true;
            }
        }
        return false;
    }

    /**
     * 清空障碍物容器
     */
    public void clearBarriers() {
        this.barriers.clear();
    }
}
